package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.PveInfo;
import com.baomidou.ant.entity.QuestionInfo;
import com.baomidou.ant.entity.User;
import com.baomidou.ant.mapper.PveInfoMapper;
import com.baomidou.ant.service.IPveInfoService;
import com.baomidou.ant.service.IQuestionInfoService;
import com.baomidou.ant.service.IUserService;
import com.baomidou.ant.utils.MyStringUtils;
import com.baomidou.ant.vo.Request.RequestChoiceInfoVo;
import com.baomidou.ant.vo.Request.RequestPassEventVo;
import com.baomidou.ant.vo.Response.ResponseGetQuestionVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@Service
public class PveInfoServiceImpl extends ServiceImpl<PveInfoMapper, PveInfo> implements IPveInfoService {
    @Autowired
    private IQuestionInfoService questionInfoService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IPveInfoService pveInfoService;


    @Override
    public List<ResponseGetQuestionVo> getQuestion(Integer id) {
        //定义三个属性，简单，中等，困难
        int easy=0;int normal=0;int hard=0;
        //先得到用户信息，最高挑战记录
        User user=userService.getById(id);
        //得到用户pve信息
        QueryWrapper<PveInfo> queryWrapper=new QueryWrapper<>();
        PveInfo pveInfo=pveInfoService.getOne(queryWrapper.eq("user_id",id));
        //根据最高关卡评定出题机制
        if(user.getHighestLevel()<=10){
            easy=10;
        }else if(user.getHighestLevel()>10&&user.getHighestLevel()<=20){
            easy=9;normal=1;
        }else if(user.getHighestLevel()>20&&user.getHighestLevel()<=30){
            easy=8;normal=2;
        }else if(user.getHighestLevel()>30&&user.getHighestLevel()<=40){
            easy=7;normal=3;
        }else if(user.getHighestLevel()>40&&user.getHighestLevel()<=50){
            easy=6;normal=3;hard=1;
        }else if(user.getHighestLevel()>50&&user.getHighestLevel()<=60){
            easy=5;normal=4;hard=1;
        }else if(user.getHighestLevel()>60&&user.getHighestLevel()<=70){
            easy=4;normal=5;hard=1;
        }else if(user.getHighestLevel()>70&&user.getHighestLevel()<=80){
            easy=3;normal=5;hard=2;
        }else if(user.getHighestLevel()>80&&user.getHighestLevel()<=90){
            easy=2;normal=5;hard=3;
        }else if(user.getHighestLevel()>90&&user.getHighestLevel()<=100){
            easy=1;normal=5;hard=4;
        }else if(user.getHighestLevel()>100&&user.getHighestLevel()<=120){
            normal=5;hard=5;
        }else if(user.getHighestLevel()>120&&user.getHighestLevel()<=150){
            normal=4;hard=6;
        }else if(user.getHighestLevel()>150&&user.getHighestLevel()<=200){
            normal=2;hard=8;
        }else if(user.getHighestLevel()>200){
            hard=10;
        }
        List<ResponseGetQuestionVo> voList=new ArrayList<>();
        //根据难度系数来出题
        //先构造3个条件查询器,分别拿出简单中等和困难题(所有的)
        QueryWrapper<QuestionInfo> queryWrapperE=new QueryWrapper<>();
        queryWrapperE.eq("question_level","简单");
        List<QuestionInfo> easyQuestion=questionInfoService.list(queryWrapperE);
        QueryWrapper<QuestionInfo> queryWrapperN=new QueryWrapper<>();
        queryWrapperN.eq("question_level","中等");
        List<QuestionInfo> normalQuestion=questionInfoService.list(queryWrapperN);
        QueryWrapper<QuestionInfo> queryWrapperH=new QueryWrapper<>();
        queryWrapperH.eq("question_level","困难");
        List<QuestionInfo> hardQuestion=questionInfoService.list(queryWrapperH);
        //再根据用户的pve信息,得到用户已经做过的题目
        int[] easyArr= MyStringUtils.toInt(pveInfo.getEasy());//做过的简单题
        int[] normalArr= MyStringUtils.toInt(pveInfo.getNormal());//做过的中等题
        int[] hardArr= MyStringUtils.toInt(pveInfo.getHard());//做过的难题
        //依次存入list中
        List<Integer> easyList = Arrays.stream(easyArr).boxed().collect(Collectors.toList());
        List<Integer> normalList= Arrays.stream(normalArr).boxed().collect(Collectors.toList());
        List<Integer> hardList= Arrays.stream(hardArr).boxed().collect(Collectors.toList());
        //依次拿简单题，中等题，困难题
        int e = 0,n=0,h=0;
        if(easy>0){
            for(QuestionInfo questionInfo:easyQuestion){
                if(ObjectUtils.isEmpty(questionInfo))break;
                if(easyList.contains(questionInfo.getId())){
                    continue;
                }else {
                    ResponseGetQuestionVo vo=new ResponseGetQuestionVo();
                    BeanUtils.copyProperties(questionInfo, vo);
                    vo.setNowLevel(user.getHighestLevel()+1);
                    voList.add(vo);
                    e=e+1;
                }
                if(e>=easy)
                    break;//拿到足够的简单题就弹出循环
            }
        }
        if(normal>0){
            for(QuestionInfo questionInfo:normalQuestion){
                if(ObjectUtils.isEmpty(questionInfo))break;
                if(normalList.contains(questionInfo.getId())){
                    continue;
                }else{
                    ResponseGetQuestionVo vo=new ResponseGetQuestionVo();
                    BeanUtils.copyProperties(questionInfo, vo);
                    vo.setNowLevel(user.getHighestLevel()+1);
                    voList.add(vo);
                    n=n+1;
                }
                if(n>=normal)
                    break;//拿到足够的简单题就弹出循环
            }
        }
        if(hard>0){
            for(QuestionInfo questionInfo:hardQuestion){
                if(ObjectUtils.isEmpty(questionInfo))break;
                if(hardList.contains(questionInfo.getId())){
                    continue;
                }else{
                    ResponseGetQuestionVo vo=new ResponseGetQuestionVo();
                    BeanUtils.copyProperties(questionInfo, vo);
                    vo.setNowLevel(user.getHighestLevel()+1);
                    voList.add(vo);
                    h=h+1;
                }if(h>=hard)
                    break;//拿到足够的简单题就弹出循环
            }
        }
        //拿题完毕
        return voList;
    }

    @Override
    public boolean trueOrFalse(RequestChoiceInfoVo requestChoiceInfoVo) {
        //根据题目id找到对应题目
        QuestionInfo questionInfo=questionInfoService.getById(requestChoiceInfoVo.getQuestionId());
        //对比答案,返回结果
        return requestChoiceInfoVo.getAnswer().equals(questionInfo.getRightItem());
    }

    @Override
    public void passEvent(RequestPassEventVo requestPassEventVo) {
        //找到对应的PVE纪录
        QueryWrapper<PveInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id", requestPassEventVo.getUserId());
        PveInfo pveInfo=pveInfoService.getOne(queryWrapper);
        //这道题做过了，记下来
        for(int i: requestPassEventVo.getQuestionId()){
            QuestionInfo questionInfo=questionInfoService.getById(i);
            if(Objects.equals(questionInfo.getQuestionLevel(), "简单")){
                //拼接字符串
                pveInfo.setEasy(MyStringUtils.toString(i,pveInfo.getEasy()));
            }
            if(Objects.equals(questionInfo.getQuestionLevel(), "中等")){
                //拼接字符串
                pveInfo.setNormal(MyStringUtils.toString(i,pveInfo.getNormal()));
            }
            if(Objects.equals(questionInfo.getQuestionLevel(), "困难")){
                //拼接字符串
                pveInfo.setHard(MyStringUtils.toString(i,pveInfo.getHard()));
            }
        }
        //保存
        pveInfoService.saveOrUpdate(pveInfo);
        //更新最高纪录并得到积分
        User user=userService.getById(requestPassEventVo.getUserId());
        user.setHighestLevel(user.getHighestLevel()+1);
        user.setPoints(user.getPoints()+user.getHighestLevel()*30);
        userService.saveOrUpdate(user);
    }
}
